package com.example.science.config;

import com.example.science.interceptor.LoginInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDate;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private  LoginInterceptor loginInterceptor;
//    @Resource
//    private PermissionInterceptor permissionInterceptor;
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 所有接口
                .allowCredentials(true) // 是否发送 Cookie
                .allowedOrigins("*") // 支持跨域
                .allowedMethods(new String[]{"GET", "POST", "PUT", "DELETE"})
                .allowedHeaders("*")//前端允许的请求头
                .exposedHeaders("*");//后端允许响应跨域的请求头
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")//拦截所有
                .excludePathPatterns("/userInfo/adminLogin","/user/sendRegisterCode","/userInfo/userRegister")//白名单
                .excludePathPatterns("/bookType/selectTypeTree")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
//        registry.addInterceptor(permissionInterceptor)
//                .addPathPatterns("/**")//拦截所有
//                .excludePathPatterns("/userInfo/adminLogin","/userInfo/sendRegisterCode","/userInfo/userRegister")//白名单
//                .excludePathPatterns("/bookType/selectTypeTree")
//                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }
    @Override
    public void addFormatters(FormatterRegistry registry){
        registry.addConverter(new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String s) {

                return LocalDate.parse(s);
            }
        });
    }
}
